package com.safe.first.application.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Mickey
 * @since 2022/6/16
 **/
public class ServerSocketTest {

    public static void main(String[] args) {
        try {

            // 初始化服务端socket并且绑定9999端口

            ServerSocket serverSocket = new ServerSocket(9999);

            //等待客户端的连接

            Socket socket = serverSocket.accept();

            //获取输入流

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //读取一行数据

            String str;

            while ((str = bufferedReader.readLine()) != null) {
                //输出打印

                System.out.println(str);

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
